﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DNA.Mvc.Services;
using DNA.OpenAPI.Rss;

namespace DNA.Mvc.Syndication
{
    public class WebPageRssItemsGenerator : RssItemsGeneratorBase
    {
        private WebSiteContext context;
        public WebPageRssItemsGenerator(WebSiteContext webContext) { context = webContext; }

        protected override IEnumerable<RssItem> OnCreateRssItems(IDictionary<string, object> data)
        {
            var pages = context.Web.WebPages.Where(p => p.AllowAnonymous).OrderBy(p => p.ID);
            var items = new List<RssItem>();

            foreach (var page in pages)
            {
                if ((!page.ShowInMenu) || (page.IsShared && page.IgnoreRouteKeys!=null))
                    continue;

                items.Add(new RssItem()
                {
                    Title = page.Title,
                    Description = page.Description,
                    Author = page.Owner,
                    Link = UrlResolver.Resolve(context.HttpContext, page),
                    Guid = new RssGuid() { IsPermaLink = true, Value = UrlResolver.Resolve(context.HttpContext, page) },
                    PubDate = page.Created.ToUniversalTime().ToString("r")
                });
            }
            return items;
        }
    }
}